﻿using alwitTest.FileSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace alwitTest.YX
{
    internal class PotIntType
    {
        private int intType;
        private object m_criSectionObject = new object();

        public int GetIntType()
        {
            lock (this.m_criSectionObject)
            {
                return intType;
            }
        }

        public void SetIntType(int dataImport)
        {
            lock (this.m_criSectionObject)
            {
                intType = dataImport;
            }
        }
    }

    internal class PotSyncData
    {
        byte m_dataValue;
        object m_criSection = new object();

        public byte PocellDataValue
        {
            get
            {
                lock (this.m_criSection)
                {
                    return this.m_dataValue;
                }
            }
            set
            {
                lock (this.m_criSection)
                {
                    this.m_dataValue = value;
                }
            }
        }

    }

    internal class AnodeSyncData
    {
        float m_dataValue;
        object m_criSection = new object();

        public float AnodeDataValue
        {
            get
            {
                lock (this.m_criSection)
                {
                    return this.m_dataValue;
                }
            }
            set
            {
                lock (this.m_criSection)
                {
                    this.m_dataValue = value;
                }
            }
        }

    }

    internal class AnodeDynamicDataBuffer
    {
        public AnodeSyncData[] m_mainAnodeData;
        public AnodeSyncData[] m_auxiliaryAnodeData;
        public AnodeSyncData[] m_sideAnodeData;

        public AnodeDynamicDataBuffer()
        {
            this.m_mainAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];
            this.m_auxiliaryAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];
            this.m_sideAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];

            for (int j = 0; j < CommNetConstants.DefaultAnodeNumber; j++)
            {
                this.m_mainAnodeData[j] = new AnodeSyncData();
                this.m_auxiliaryAnodeData[j] = new AnodeSyncData();
                this.m_sideAnodeData[j] = new AnodeSyncData();
            }

        }
    }

    internal class YXBufferPacket
    {
        private byte[] m_buffer;
        private string m_userKey = "";
        private int m_offset = 0;
        private int m_length;
        private bool m_dirtySymbol = false;

        private byte[] m_ipBuffer = new byte[4];

        public YXBufferPacket(int bufferSize)
        {
            this.m_buffer = new byte[bufferSize];
            this.m_length = this.m_buffer.Length;
        }

        public byte[] PacketBuffer
        {
            get
            {
                return this.m_buffer;
            }
        }

        public int PacketOffset
        {
            get
            {
                return this.m_offset;
            }
            set
            {
                this.m_offset = value;
            }

        }

        public int PacketLength
        {
            get
            {
                return this.m_length;
            }
            set
            {
                this.m_length = value;
            }
        }

        public bool PacketDirtySymbol
        {
            get
            {
                return this.m_dirtySymbol;
            }
            set
            {
                this.m_dirtySymbol = value;
            }
        }

        public byte[] PacketIPBuffer
        {
            get
            {
                return this.m_ipBuffer;
            }
        }

        public string PacketUserKey
        {
            get
            {
                return m_userKey;
            }
            set
            {
                m_userKey = value;
            }
        }

        public void SetIPBuffer(int index, byte value)
        {
            if (index < this.m_ipBuffer.Length)
            {
                this.m_ipBuffer[index] = value;
            }
        }

    }

    internal static class CommToolFunction
    {
        public static bool GetIPByte(string ipText, byte[] ipByte)
        {
            int ipPos;
            string tempText = "";

            tempText = ipText.Trim();
            ipPos = tempText.IndexOf(".", 1);
            if (ipPos == 0)
            {
                return false;
            }
            else
            {
                ipByte[0] = Convert.ToByte(tempText.Substring(0, ipPos));
                tempText = tempText.Substring(ipPos + 1);
                ipPos = tempText.IndexOf(".", 0);
                if (ipPos == 0)
                {
                    return false;
                }
                else
                {
                    ipByte[1] = Convert.ToByte(tempText.Substring(0, ipPos));
                    tempText = tempText.Substring(ipPos + 1);
                    ipPos = tempText.IndexOf(".", 0);
                    if (ipPos == 0)
                    {
                        return false;
                    }
                    else
                    {
                        ipByte[2] = Convert.ToByte(tempText.Substring(0, ipPos));
                        tempText = tempText.Substring(ipPos + 1);
                        ipPos = tempText.IndexOf(".", 0);
                        if (ipPos == -1)
                        {
                            ipByte[3] = Convert.ToByte(tempText);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }

                }
            }

        }

        public static float GetRealAnodeVoltValue(int dataValue)
        {
            float dataTempValue;

            dataTempValue = dataValue;
            dataTempValue = (dataTempValue - 1250) / 100;
            return dataTempValue;

        }

        internal static string GetMessage_trouble(PotSyncData[] dataInfo)
        {
            string tmpText = "";
            string stateText = "";

            tmpText = SolveTroubleInfo1(dataInfo[0].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveTroubleInfo2(dataInfo[1].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveTroubleInfo3(dataInfo[2].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveTroubleInfo4(dataInfo[3].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            return stateText;

        }

        internal static string GetMessage_state(PotSyncData[] dataInfo, byte display3, byte switch2)
        {
            string tmpText = "";
            string stateText = "";

            tmpText = SolveDisplayInfo1(dataInfo[0].PocellDataValue, display3);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveDisplayInfo2(dataInfo[1].PocellDataValue, switch2);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveDisplayInfo3(dataInfo[2].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            tmpText = SolveDisplayInfo4(dataInfo[3].PocellDataValue);
            if (tmpText != "")
            {
                if (stateText != "")
                {
                    stateText = stateText + "," + tmpText;
                }
                else
                {
                    stateText = tmpText;
                }
            }

            return stateText;

        }

        internal static string SolveDisplayInfo1(byte protocolByte, byte display3)
        {
            string potIDSymbol = "";
            byte jk;
            byte j;

            jk = (byte)(protocolByte & 4);//阳降
            if (jk != 0)
            {
                j = (byte)(display3 & 16);
                if (j != 0)//手动
                {
                    potIDSymbol = potIDSymbol + "手动阳降,";
                }
                else
                {
                    potIDSymbol = potIDSymbol + "自动阳降,";
                }
            }

            jk = (byte)(protocolByte & 8);//阳升
            if (jk != 0)
            {
                j = (byte)(display3 & 16);
                if (j != 0)//手动
                {
                    potIDSymbol = potIDSymbol + "手动阳升,";
                }
                else
                {
                    potIDSymbol = potIDSymbol + "自动阳升,";
                }
            }

            jk = (byte)(protocolByte & 64);//下料器故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "下料器故障,";
            }

            jk = (byte)(protocolByte & 128);//效应
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "效应,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveDisplayInfo2(byte protocolByte, byte switch2)
        {
            string potIDSymbol = "";
            byte jk;
            byte j;

            //显示信息2（50）
            jk = (byte)(protocolByte & 1);//大下料
            if (jk != 0)
            {
                j = (byte)(switch2 & 4);//手动下料开关
                if (j != 0)//手动
                {
                    potIDSymbol = potIDSymbol + "手动大下料,";
                }
                else
                {
                    potIDSymbol = potIDSymbol + "自动大下料,";
                }
            }

            jk = (byte)(protocolByte & 2);//小下料
            if (jk != 0)
            {
                j = (byte)(switch2 & 4);//手动下料开关
                if (j != 0)//手动
                {
                    potIDSymbol = potIDSymbol + "手动小下料,";
                }
                else
                {
                    potIDSymbol = potIDSymbol + "自动小下料,";
                }
            }

            jk = (byte)(protocolByte & 4);//出铝
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "出铝,";
            }

            jk = (byte)(protocolByte & 8);//抬母线
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "抬母线,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveDisplayInfo3(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;
            byte j;

            //显示信息3（51）
            jk = (byte)(protocolByte & 1);//氟盐
            if (jk != 0)
            {
                j = (byte)(protocolByte & 16);//手动
                if (j != 0)//手动
                {
                    potIDSymbol = potIDSymbol + "手动氟盐,";
                }
                else
                {
                    potIDSymbol = potIDSymbol + "自动氟盐,";
                }
            }

            jk = (byte)(protocolByte & 2);//单打壳
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "单打壳,";
            }

            jk = (byte)(protocolByte & 32);//纯手动
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "纯手动,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveDisplayInfo4(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;

            //显示信息4（52）
            jk = (byte)(protocolByte & 1);//保留
            if (jk != 0)
            {
            }
            else
            {
            }

            jk = (byte)(protocolByte & 2);//保留
            if (jk != 0)
            {
            }
            else
            {
            }

            jk = (byte)(protocolByte & 4);//机器故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "机器故障,";
            }

            jk = (byte)(protocolByte & 8);//自诊断
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "堵料,";
            }

            jk = (byte)(protocolByte & 16);//采样诊断
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "壳头包,";
            }

            jk = (byte)(protocolByte & 64);//边加工
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "边加工,";
            }

            jk = (byte)(protocolByte & 128);//换极
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "换极,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveTroubleInfo1(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;

            //故障信息1（45）
            jk = (byte)(protocolByte & 1);//定时超
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "定时超,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 2);//电源跳
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "电源跳,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 4);//保留
            if (jk != 0)
            {
            }
            else
            {
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 8);//母线上限
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "母线上限,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 16);//母线下限
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "母线下限,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 32);//主粘连
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "主粘连,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 64);//正粘连
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "正粘连,";
            }

            //故障信息1（45）
            jk = (byte)(protocolByte & 128);//反粘连
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "反粘连,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveTroubleInfo2(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;

            //故障信息2（46）
            jk = (byte)(protocolByte & 1);//主未合
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "主未合,";
            }

            jk = (byte)(protocolByte & 2);//正未合
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "正未合,";
            }

            jk = (byte)(protocolByte & 4);//反未合
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "反未合,";
            }

            jk = (byte)(protocolByte & 8);//交流口
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "交流口,";
            }

            jk = (byte)(protocolByte & 16);//定时长
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "定时长,";
            }

            jk = (byte)(protocolByte & 32);//定时短
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "定时短,";
            }

            jk = (byte)(protocolByte & 64);//定时器复位
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "定时器复位,";
            }

            jk = (byte)(protocolByte & 128);//升降同时
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "升降同时,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveTroubleInfo3(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;

            //故障信息3（101）
            jk = (byte)(protocolByte & 1);//出铝端母线方向错误代替手动开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "手动开关故障,";
            }

            jk = (byte)(protocolByte & 2);//槽中部母线方向错误代替阳升开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "阳升开关故障,";
            }

            jk = (byte)(protocolByte & 4);//烟道端母线方向错误代替加料开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "加料开关故障,";
            }

            jk = (byte)(protocolByte & 8);//出铝端母线测量错误代替参数下开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "参数下开关故障,";
            }

            jk = (byte)(protocolByte & 16);//槽中部母线测量错误代替参数上开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "参数上开关故障,";
            }

            jk = (byte)(protocolByte & 32);//烟道端母线测量错误代替复位开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "复位开关故障,";
            }

            jk = (byte)(protocolByte & 64);//阳降开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "阳降开关故障,";
            }

            jk = (byte)(protocolByte & 128);//边加工开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "边加工开关故障,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

        internal static string SolveTroubleInfo4(byte protocolByte)
        {
            string potIDSymbol = "";
            byte jk;

            //jk = (byte)(protocolByte & 2);//阳极升降不保护
            //if (jk != 0)
            //{
            //    potIDSymbol = potIDSymbol + "阳极升降不保护,";
            //}

            jk = (byte)(protocolByte & 4);//操作版故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "操作板故障,";
            }

            jk = (byte)(protocolByte & 8);//VF板故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "VF板故障,";
            }

            jk = (byte)(protocolByte & 16);//抬母线开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "抬母线开关故障,";
            }

            jk = (byte)(protocolByte & 32);//打壳开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "打壳开关故障,";
            }

            jk = (byte)(protocolByte & 64);//出铝开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "出铝开关故障,";
            }

            jk = (byte)(protocolByte & 128);//换极开关故障
            if (jk != 0)
            {
                potIDSymbol = potIDSymbol + "换极开关故障,";
            }

            if (potIDSymbol.Length == 0)
            {
            }
            else
            {
                potIDSymbol = potIDSymbol.Substring(0, potIDSymbol.Length - 1);
            }

            return potIDSymbol;

        }

    }

    internal class HexConvert
    {

        protected static char[] CHAR = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

        protected static String toHex(long value, int bytes)
        {

            int max = (bytes << 1) - 1;
            char[] chars = new char[bytes << 1];

            for (int i = (bytes << 1) - 1; i >= 0; i--)
            {
                chars[max - i] = CHAR[(int)((value >> (4 * i)) & 0xF)];
            }

            return new String(chars);

        }

        public static String toHex(byte value)
        {
            return toHex(value, 1);
        }

        public static String toHex(char value)
        {
            return toHex(value, 2);
        }

        public static String toHex(short value)
        {
            return toHex(value, 2);
        }

        public static String toHex(int value)
        {
            return toHex(value, 4);
        }

        public static String toHex(long value)
        {
            return toHex(value, 8);
        }

        public static String toHex(byte[] value)
        {

            StringBuilder buf = new StringBuilder();
            for (int i = 0; i < value.Length; i++)
            {
                buf.Append(toHex(value[i]));
            }

            return buf.ToString();

        }

        public static String toHex(char[] value)
        {
            StringBuilder buf = new StringBuilder();
            for (int i = 0; i < value.Length; i++)
            {
                buf.Append(toHex(value[i]));
            }
            return buf.ToString();
        }

    }

    internal class ResponseBufferPacket
    {
        private byte[] m_buffer;
        private int m_offset = 0;
        private int m_length;
        private bool m_dirtySymbol = false;

        public ResponseBufferPacket(int bufferSize)
        {
            this.m_buffer = new byte[bufferSize];
            this.m_length = this.m_buffer.Length;
        }

        public byte[] PacketBuffer
        {
            get
            {
                return this.m_buffer;
            }
        }

        public int PacketOffset
        {
            get
            {
                return this.m_offset;
            }
            set
            {
                this.m_offset = value;
            }

        }

        public int PacketLength
        {
            get
            {
                return this.m_length;
            }
            set
            {
                this.m_length = value;
            }
        }

        public bool PacketDirtySymbol
        {
            get
            {
                return this.m_dirtySymbol;
            }
            set
            {
                this.m_dirtySymbol = value;
            }
        }
    }

    internal class SendQueueBufferPacket
    {
        private ResponseBufferPacket[] m_queueBuffer;
        private object m_syncRoot = new object();
        private int m_inputPointer = 0;
        private int m_originPointer = 0;
        private int m_sendPointer = 0;

        public SendQueueBufferPacket(int capacity, int bufferSize)
        {
            this.m_queueBuffer = new ResponseBufferPacket[capacity];

            for (int i = 0; i < m_queueBuffer.Length; i++)
            {
                m_queueBuffer[i] = new ResponseBufferPacket(bufferSize);
            }
        }

        internal void Push_direct(byte[] dataBuffer, int offset, int length)
        {
            lock (m_syncRoot)
            {
                m_originPointer = m_inputPointer;
                m_queueBuffer[m_inputPointer].PacketOffset = offset;
                m_queueBuffer[m_inputPointer].PacketLength = length;
                m_queueBuffer[m_inputPointer].PacketDirtySymbol = true;
                Buffer.BlockCopy(dataBuffer, offset, m_queueBuffer[m_inputPointer].PacketBuffer, m_queueBuffer[m_inputPointer].PacketOffset, length);

                if ((m_inputPointer + 1) >= m_queueBuffer.Length)
                {
                    m_inputPointer = 0;
                }
                else
                {
                    m_inputPointer = m_inputPointer + 1;
                }

            }
        }

        private ResponseBufferPacket Pop()
        {
            lock (m_syncRoot)
            {
                if (m_queueBuffer[m_sendPointer].PacketDirtySymbol == false)
                {
                    return null;
                }
                else
                {
                    return m_queueBuffer[m_sendPointer];
                }

            }
        }

        internal void PopAll()
        {
            lock (m_syncRoot)
            {
                for (int i = 0; i < m_queueBuffer.Length; i++)
                {
                    m_queueBuffer[i].PacketDirtySymbol = false;
                }

                this.m_inputPointer = 0;
                this.m_sendPointer = 0;
            }
        }

        internal int PopPacket(ResponseBufferPacket respPacket)
        {
            lock (m_syncRoot)
            {
                if (m_queueBuffer[m_sendPointer].PacketDirtySymbol == false)
                {
                    return -1;
                }
                else
                {
                    respPacket.PacketOffset = m_queueBuffer[m_sendPointer].PacketOffset;
                    respPacket.PacketLength = m_queueBuffer[m_sendPointer].PacketLength;
                    respPacket.PacketDirtySymbol = m_queueBuffer[m_sendPointer].PacketDirtySymbol;
                    Buffer.BlockCopy(m_queueBuffer[m_sendPointer].PacketBuffer, m_queueBuffer[m_sendPointer].PacketOffset, respPacket.PacketBuffer, respPacket.PacketOffset, m_queueBuffer[m_sendPointer].PacketLength);

                    m_queueBuffer[m_sendPointer].PacketDirtySymbol = false;
                    if ((m_sendPointer + 1) >= m_queueBuffer.Length)
                    {
                        m_sendPointer = 0;
                    }
                    else
                    {
                        m_sendPointer = m_sendPointer + 1;
                    }

                    return 1;
                }

            }
        }

        internal bool IsQueueBufferEffect()
        {
            bool tempSymbol = false;

            lock (m_syncRoot)
            {
                tempSymbol = this.m_queueBuffer[this.m_originPointer].PacketDirtySymbol;
            }

            return tempSymbol;

        }

        internal int QueueBufferCount
        {
            get
            {
                return m_queueBuffer.Length;
            }
        }

    }

    internal class ReceiveQueueBuffer_packet
    {
        private YXBufferPacket[] m_queueBuffer;
        private object m_syncRoot = new object();
        private int m_inputPointer = 0;
        private int m_outputPointer = 0;
        private byte[] m_ipBuffer = new byte[4];

        public ReceiveQueueBuffer_packet(int capacity, int bufferSize)
        {
            this.m_queueBuffer = new YXBufferPacket[capacity];

            for (int i = 0; i < m_queueBuffer.Length; i++)
            {
                m_queueBuffer[i] = new YXBufferPacket(bufferSize);
            }
        }

        internal void Push_All(byte[] buffers, int offset_src, int length, int bufferSize, string userKey)
        {
            int offset;
            int jk;

            lock (m_syncRoot)
            {
                //if (Utility.GetIPByte(userKey, this.m_ipBuffer) == false)
                //{
                //    for (int i = 0; i < this.m_ipBuffer.Length; i++)
                //    {
                //        this.m_ipBuffer[i] = 0;
                //    }
                //}

                jk = (int)(length / bufferSize);
                for (int i = 0; i < jk; i++)
                {
                    offset = offset_src + bufferSize * i;

                    m_queueBuffer[m_inputPointer].PacketOffset = 0;
                    m_queueBuffer[m_inputPointer].PacketLength = bufferSize;
                    m_queueBuffer[m_inputPointer].PacketDirtySymbol = true;
                    Buffer.BlockCopy(buffers, offset, m_queueBuffer[m_inputPointer].PacketBuffer, m_queueBuffer[m_inputPointer].PacketOffset, bufferSize);

                    for (int j = 0; j < this.m_ipBuffer.Length; j++)
                    {
                        m_queueBuffer[m_inputPointer].SetIPBuffer(j, this.m_ipBuffer[j]);
                    }

                    //-----------------------//
                    if ((m_inputPointer + 1) >= m_queueBuffer.Length)
                    {
                        m_inputPointer = 0;
                    }
                    else
                    {
                        m_inputPointer = m_inputPointer + 1;
                    }

                }

            }
        }

        internal void Push(byte[] dataBuffer, int offset_src, int offset_dest, int length, string userKey)
        {
            lock (m_syncRoot)
            {
                m_queueBuffer[m_inputPointer].PacketOffset = offset_dest;
                m_queueBuffer[m_inputPointer].PacketLength = length;
                if (userKey != "")
                {
                    m_queueBuffer[m_inputPointer].PacketUserKey = userKey.Trim();
                }
                else
                {
                    m_queueBuffer[m_inputPointer].PacketUserKey = "";
                }
                m_queueBuffer[m_inputPointer].PacketDirtySymbol = true;
                Buffer.BlockCopy(dataBuffer, offset_src, m_queueBuffer[m_inputPointer].PacketBuffer, m_queueBuffer[m_inputPointer].PacketOffset, length);

                //if (Utility.GetIPByte(userKey, this.m_ipBuffer) == false)
                //{
                //    for (int i = 0; i < this.m_ipBuffer.Length; i++)
                //    {
                //        this.m_ipBuffer[i] = 0;
                //    }
                //}

                //for (int i = 0; i < this.m_ipBuffer.Length; i++)
                //{
                //    m_queueBuffer[m_inputPointer].SetIPBuffer(i, this.m_ipBuffer[i]);
                //}

                //-----------------------//
                if ((m_inputPointer + 1) >= m_queueBuffer.Length)
                {
                    m_inputPointer = 0;
                }
                else
                {
                    m_inputPointer = m_inputPointer + 1;
                }
            }
        }

        internal int PopPacket_All(byte[] dataBuffer, int bufferBound)
        {
            int offset = 0;
            int length = 0;

            lock (m_syncRoot)
            {
                for (int i = 0; i < bufferBound; i++)
                {
                    if (m_queueBuffer[m_outputPointer].PacketDirtySymbol == true)
                    {
                        Buffer.BlockCopy(m_queueBuffer[m_outputPointer].PacketBuffer, 0, dataBuffer, offset, m_queueBuffer[m_outputPointer].PacketLength);

                        offset = offset + m_queueBuffer[m_outputPointer].PacketLength;
                        length = length + m_queueBuffer[m_outputPointer].PacketLength;

                        m_queueBuffer[m_outputPointer].PacketDirtySymbol = false;

                        if ((m_outputPointer + 1) >= m_queueBuffer.Length)
                        {
                            m_outputPointer = 0;
                        }
                        else
                        {
                            m_outputPointer = m_outputPointer + 1;
                        }

                    }

                }

                return length;

            }

        }

        internal int PopPacket(YXBufferPacket bufferPacket)
        {
            int tempPointer;

            lock (m_syncRoot)
            {
                if (m_queueBuffer[m_outputPointer].PacketDirtySymbol == false)
                {
                    return -1;
                }
                else
                {
                    tempPointer = m_outputPointer;
                    m_queueBuffer[m_outputPointer].PacketDirtySymbol = false;

                    if ((m_outputPointer + 1) >= m_queueBuffer.Length)
                    {
                        m_outputPointer = 0;
                    }
                    else
                    {
                        m_outputPointer = m_outputPointer + 1;
                    }

                    bufferPacket.PacketOffset = m_queueBuffer[tempPointer].PacketOffset;
                    bufferPacket.PacketLength = m_queueBuffer[tempPointer].PacketLength;
                    bufferPacket.PacketDirtySymbol = m_queueBuffer[tempPointer].PacketDirtySymbol;
                    bufferPacket.PacketUserKey = m_queueBuffer[tempPointer].PacketUserKey;
                    Buffer.BlockCopy(m_queueBuffer[tempPointer].PacketBuffer, m_queueBuffer[tempPointer].PacketOffset, bufferPacket.PacketBuffer, bufferPacket.PacketOffset, m_queueBuffer[tempPointer].PacketLength);

                    return 1;

                }

            }
        }

        internal void PopAllPacket()
        {
            lock (m_syncRoot)
            {
                for (int j = 0; j < this.m_queueBuffer.Length; j++)
                {
                    this.m_queueBuffer[j].PacketDirtySymbol = false;
                }
            }
        }

        internal int QueueBufferCount
        {
            get
            {
                return m_queueBuffer.Length;
            }
        }

    }

    internal class CommStreamData
    {
        private byte m_channelIndex = 0;
        private byte m_commHighIndex = 0;
        private byte m_commLowIndex = 0;
        private byte m_packetType = 0;
        private bool[] m_filterSymbol = new bool[4];

        public string[] m_filterPackets = new string[CommNetConstants.DataFilterPacketNumber];
        public int m_filterPacketIndex = 0;
        private object m_filterCriSection = new object();

        public static int m_gasSmokeData;
        public int[] m_tempTestData = new int[256];
        internal PotSyncData[] m_potSwitchInfo = new PotSyncData[2];
        internal PotSyncData[] m_potDisplayInfo = new PotSyncData[4];
        internal PotSyncData[] m_potTroubleInfo = new PotSyncData[4];

        public int m_packetIndex;
        public int m_effectPacketLength;
        public byte[] m_packetDataBuffer = new byte[2048];
        public int m_potRealVolt;
        public int m_potSerialCurrent;

        public int m_buffetCount;
        private object m_criBufferSolve = new object();
        private List<byte> m_buffer = new List<byte>(2048);
        private byte[] m_buffer_proxy = new byte[2048];
        private byte[] m_dataBuffer_can = new byte[CommNetConstants.ClientPacketSize_Remote];

        private ReceiveQueueBuffer_packet m_potRecQueue_anode;
        private YXBufferPacket m_yxBufferPacket;

        private AnodeSyncData[] m_mainAnodeData;
        private AnodeSyncData[] m_auxiliaryAnodeData;
        private AnodeSyncData[] m_sideAnodeData;

        private AnodeDynamicDataBuffer[] m_anodeDynamicDataBuffer;

        private int m_anodePodIndex = 0;
        private object m_criSection = new object();

        private bool m_commCheckSymbol = false;
        private object m_criCommSection = new object();

        //--------------------
        private int m_dynamicCountIndex = 0;//动态曲线循环计数用
        private int m_dynamicCountIndexOrigin = 0;
        private Object m_DynamicCurveCriSection = new object();
        private Object m_DynamicCurveCriSectionOrigin = new object();

        private int GetDynamicCountIndex()
        {
            lock (m_DynamicCurveCriSection)
            {
                return m_dynamicCountIndex;
            }
        }
        private void SetDynamicCountIndex(int dynamicCountIndex)
        {
            lock (m_DynamicCurveCriSection)
            {
                m_dynamicCountIndex = dynamicCountIndex;
            }
        }

        public int GetDynamicCountIndexOrigin()
        {
            lock (m_DynamicCurveCriSectionOrigin)
            {
                return m_dynamicCountIndexOrigin;
            }
        }
        private void SetDynamicCountIndexOrigin(int dynamicCountIndex)
        {
            lock (m_DynamicCurveCriSectionOrigin)
            {
                m_dynamicCountIndexOrigin = dynamicCountIndex;
            }
        }

        private System.Timers.Timer tmrSampleProcess = new System.Timers.Timer();
        private int tmrSampleProcessIsRunning = 0;

        private System.Timers.Timer tmrDataExchange = new System.Timers.Timer();
        private int tmrDataExchangeIsRunning = 0;

        public CommStreamData()
        {
            for (int j = 0; j < this.m_filterPackets.Length; j++)
            {
                this.m_filterPackets[j] = "";
            }

            for (int j = 0; j < this.m_potDisplayInfo.Length; j++)
            {
                this.m_potDisplayInfo[j] = new PotSyncData();
                this.m_potTroubleInfo[j] = new PotSyncData();
            }
            for (int j = 0; j < this.m_potSwitchInfo.Length; j++)
            {
                this.m_potSwitchInfo[j] = new PotSyncData();
            }

            this.m_mainAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];
            this.m_auxiliaryAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];
            this.m_sideAnodeData = new AnodeSyncData[CommNetConstants.DefaultAnodeNumber];
            for (int j = 0; j < CommNetConstants.DefaultAnodeNumber; j++)
            {
                this.m_mainAnodeData[j] = new AnodeSyncData();
                this.m_auxiliaryAnodeData[j] = new AnodeSyncData();
                this.m_sideAnodeData[j] = new AnodeSyncData();
            }

            this.m_anodeDynamicDataBuffer = new AnodeDynamicDataBuffer[CommNetConstants.AnodeDynamicBufferLength];
            for (int j = 0; j < this.m_anodeDynamicDataBuffer.Length; j++)
            {
                this.m_anodeDynamicDataBuffer[j] = new AnodeDynamicDataBuffer();
            }

            this.m_yxBufferPacket = new YXBufferPacket(CommNetConstants.ClientPacketSize_Remote);
            this.m_potRecQueue_anode = new ReceiveQueueBuffer_packet(CommNetConstants.ReceiveBufferCapacity_potcell, CommNetConstants.ClientPacketSize_Remote);

            //Timer
            this.tmrSampleProcess.Interval = 10;
            this.tmrSampleProcess.AutoReset = true;
            this.tmrSampleProcess.Elapsed += new System.Timers.ElapsedEventHandler(tmrSampleProcess_Elapsed);

            this.tmrDataExchange.Interval = 2000;
            this.tmrDataExchange.AutoReset = true;
            this.tmrDataExchange.Elapsed += new System.Timers.ElapsedEventHandler(tmrDataExchange_Elapsed);

        }

        public void StreamDataStart()
        {
            this.tmrSampleProcess.Enabled = true;
            //this.tmrDataExchange.Enabled = true;
        }

        private void tmrSampleProcess_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (Interlocked.Exchange(ref this.tmrSampleProcessIsRunning, 1) == 0)
                {
                    if (MainWindow.m_packetTypeMode == false)
                    {
                        DeserializePotcellData();
                    }
                    else
                    {
                        for (int i = 0; i <= 2; i++)
                        {
                            if (this.m_potRecQueue_anode.PopPacket(this.m_yxBufferPacket) == -1)
                            {
                                break;
                            }
                            else
                            {
                                SolveProtocolDataWithoutVerify(this.m_yxBufferPacket.PacketBuffer);
                            }
                        }
                    }

                    Interlocked.Exchange(ref this.tmrSampleProcessIsRunning, 0);

                }

            }
            catch (Exception ex)
            {
                FileHelperProxy.FileProcessHelper.WriteFile(ex.ToString(), "AnodeStreamData", "tmrSampleProcess_Elapsed");
            }
            finally
            {
            }

        }

        private void tmrDataExchange_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                if (Interlocked.Exchange(ref this.tmrDataExchangeIsRunning, 1) == 0)
                {
                    DynamicDataExchange();

                    Interlocked.Exchange(ref this.tmrDataExchangeIsRunning, 0);

                }
            }
            catch (Exception ex)
            {
                FileHelperProxy.FileProcessHelper.WriteFile(ex.ToString(), "AnodeStreamData", "tmrDataExchange_Elapsed");
            }
            finally
            {
            }

        }

        private void SolveProtocolData(byte[] protocolByte, int len)
        {
            byte[] dataBuffer = new byte[11];
            int tempDataValue;
            int offset = 0;
            int jk = 0;
            int tmpValue;
            int tmpData;
            int tempData;
            int tempData8;

            if (MainWindow.m_packetFilterSymbol == false)
            {
                this.m_packetIndex = protocolByte[3 + offset];
                this.m_effectPacketLength = len;
                Buffer.BlockCopy(protocolByte, 0, this.m_packetDataBuffer, 0, this.m_effectPacketLength);
            }
            else
            {
                if (MainWindow.m_packetIndexSpecial != 0)
                {
                    if (protocolByte[3 + offset] == MainWindow.m_packetIndexSpecial)
                    {
                        this.m_packetIndex = protocolByte[3 + offset];
                        this.m_effectPacketLength = len;
                        Buffer.BlockCopy(protocolByte, 0, this.m_packetDataBuffer, 0, this.m_effectPacketLength);
                    }
                }
            }

            //-----------------------//
            SolveFilterPacket(protocolByte, len);

            //-----------------------//
            if ((len % 11) == 0)
            {
                jk = len / 11;
                for (int j = 0; j < jk; j++)
                {
                    offset = j * 11;
                    switch (protocolByte[3 + offset])
                    {
                        case 2:

                            for (int jt = 0; jt < 11; jt++)
                            {
                                dataBuffer[jt] = protocolByte[jt + offset];
                            }
                            SolveAddressParaData(dataBuffer);
                            tmpData = 1;
                            break;

                        case 3:
                        case 4:

                            tempDataValue = protocolByte[4 + offset];
                            tempDataValue = tempDataValue * 256 + protocolByte[5 + offset];
                            this.m_potRealVolt = tempDataValue;

                            tempDataValue = protocolByte[6 + offset];
                            tempDataValue = tempDataValue * 256 + protocolByte[7 + offset];
                            this.m_potSerialCurrent = tempDataValue;

                            if (protocolByte[3 + offset] == 3)
                            {
                                MainWindow.m_feedControlState = protocolByte[10 + offset];
                            }
                            break;

                        case 5:
                        case 6:

                            tempData8 = protocolByte[4 + offset];
                            tempData8 = tempData8 * 256 + protocolByte[5 + offset];
                            tempData = protocolByte[6 + offset];
                            tempData = tempData * 256 + protocolByte[7 + offset];
                            if (tempData > 4000)
                            {
                                int ph = 1;
                            }
                            this.m_potDisplayInfo[3].PocellDataValue = protocolByte[10 + offset];

                            byte jp1 = (byte)(this.m_potDisplayInfo[3].PocellDataValue & 128);//换极
                            if (jp1 != 0)
                            {
                                int phh = 1;
                            }

                            break;

                        case 7:

                            this.m_potTroubleInfo[0].PocellDataValue = protocolByte[4 + offset];
                            this.m_potTroubleInfo[1].PocellDataValue = protocolByte[5 + offset];
                            this.m_potTroubleInfo[2].PocellDataValue = protocolByte[6 + offset];
                            this.m_potTroubleInfo[3].PocellDataValue = protocolByte[7 + offset];

                            this.m_potDisplayInfo[0].PocellDataValue = protocolByte[8 + offset];
                            this.m_potDisplayInfo[1].PocellDataValue = protocolByte[9 + offset];
                            this.m_potDisplayInfo[2].PocellDataValue = protocolByte[10 + offset];

                            byte jp = (byte)(this.m_potDisplayInfo[1].PocellDataValue & 4);//出铝
                            if (jp != 0)
                            {
                                int ph = 1;
                            }

                            jp = (byte)(this.m_potDisplayInfo[0].PocellDataValue & 4);//阳降
                            if (jp != 0)
                            {
                                int ph1 = 1;
                            }

                            jp = (byte)(this.m_potDisplayInfo[0].PocellDataValue & 8);//阳升
                            if (jp != 0)
                            {
                                int ph2 = 1;
                            }

                            jp = (byte)(this.m_potDisplayInfo[2].PocellDataValue & 16);//手动/自动
                            if (jp != 0)
                            {
                                int ph4 = 1;
                            }

                            break;

                        case 11:

                            this.m_potSwitchInfo[0].PocellDataValue = protocolByte[5 + offset];
                            this.m_potSwitchInfo[1].PocellDataValue = protocolByte[4 + offset];
                            jp = (byte)(this.m_potSwitchInfo[1].PocellDataValue & 16);//参数上
                            if (jp != 0)
                            {
                                int ph3 = 1;
                            }

                            int codeNumber = protocolByte[10 + offset];//丁总送上来的故障代码；

                            break;

                        case 46://人工操作帧
                            if (protocolByte[4 + offset] == 0)
                            {
                                MainWindow.m_dataToNow_autoau = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_autoad = protocolByte[6 + offset];
                                MainWindow.m_dataToNow_manau = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_manad = protocolByte[8 + offset];
                            }
                            else if (protocolByte[4 + offset] == 2)
                            {
                                MainWindow.m_dataToNow_dis_autofd[0] = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_dis_autofd[1] = protocolByte[6 + offset];
                                MainWindow.m_dataToNow_dis_autofd[2] = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_dis_autofd[3] = protocolByte[8 + offset];
                                MainWindow.m_dataToNow_dis_autofd[4] = protocolByte[9 + offset];
                                MainWindow.m_dataToNow_dis_autofd[5] = protocolByte[10 + offset];
                            }
                            else if (protocolByte[4 + offset] == 3)
                            {
                                MainWindow.m_dataToNow_dis_autofd[6] = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_dis_autofd[7] = protocolByte[6 + offset];
                            }
                            else if (protocolByte[4 + offset] == 6)
                            {
                                MainWindow.m_dataToNow_manalf = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_autofd = protocolByte[10 + offset];
                            }
                            else if (protocolByte[4 + offset] == 7)
                            {
                                MainWindow.m_dataToNow_manbr = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_manfd = protocolByte[6 + offset];
                            }

                            break;

                        case 47:
                            if (protocolByte[4 + offset] == 0)
                            {
                                MainWindow.m_dataToNow_ts_qtb = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_ts_ac = protocolByte[6 + offset];
                                MainWindow.m_dataToNow_ts_ok = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_ts_sw = protocolByte[8 + offset];
                                MainWindow.m_dataToNow_ts_pu = protocolByte[9 + offset];
                                MainWindow.m_dataToNow_ts_kd = protocolByte[10 + offset];
                            }
                            else if (protocolByte[4 + offset] == 1)
                            {
                                MainWindow.m_dataToNow_ts_pd = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_ts_fuc = protocolByte[6 + offset];
                                MainWindow.m_dataToNow_ts_brk = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_ts_au = protocolByte[8 + offset];
                                MainWindow.m_dataToNow_ts_br = protocolByte[9 + offset];
                                MainWindow.m_dataToNow_ts_man = protocolByte[10 + offset];
                            }
                            else if (protocolByte[4 + offset] == 2)
                            {
                                MainWindow.m_dataToNow_ts_fd = protocolByte[5 + offset];
                                MainWindow.m_dataToNow_ts_ad = protocolByte[6 + offset];
                                MainWindow.m_dataToNow_ts_alf = protocolByte[7 + offset];
                                MainWindow.m_dataToNow_ts_tap = protocolByte[8 + offset];
                            }

                            break;

                        case 91://温度

                            SolvePotTempData(protocolByte, offset);
                            break;

                        case 80://烟气

                            SolveSmokeGasData(protocolByte, offset);
                            break;

                        case 240:

                            int pl = 1;
                            break;


                        case 254:

                            if (protocolByte[4 + offset] == 102)
                            {
                                tmpValue = protocolByte[6 + offset];
                                tmpValue = tmpValue * 256 + protocolByte[7 + offset];
                            }
                            else if (protocolByte[4 + offset] == 123)
                            {
                                tmpValue = protocolByte[5 + offset];
                                tmpValue = tmpValue * 256 + protocolByte[6 + offset];
                            }
                            else if (protocolByte[4 + offset] == 128)
                            {
                                tmpValue = protocolByte[5 + offset];//停控时间
                                tmpValue = tmpValue * 256 + protocolByte[6 + offset];

                                tmpValue = protocolByte[7 + offset];//停料时间
                                tmpValue = tmpValue * 256 + protocolByte[8 + offset];

                                tmpValue = protocolByte[9 + offset];//定时时间
                                tmpValue = tmpValue * 256 + protocolByte[10 + offset];

                            }
                            else
                            {
                            }
                            break;

                        case 150:
                            tmpData = 1;
                            break;
                        case 151:
                            tmpData = 1;
                            break;
                        case 160:
                            tmpData = 1;
                            break;
                        case 162:
                            tmpData = 1;
                            break;
                        case 180:
                            tmpData = 1;
                            break;
                        case 192:
                            tmpData = 1;
                            break;

                        case 13:

                            tmpValue = protocolByte[4 + offset];
                            tmpValue = tmpValue * 256 + protocolByte[5 + offset];
                            break;

                        case 89:

                            if (protocolByte[4 + offset] == 5)
                            {
                                tmpValue = protocolByte[5 + offset];
                                tmpValue = tmpValue * 256 + protocolByte[6 + offset];
                                MainWindow.m_busData = tmpValue;
                            }
                            break;

                        case 31:

                            SolveAnodeData(protocolByte, offset);
                            break;

                        case 30:

                            SolveACToNowData(protocolByte, offset);
                            break;

                        default:
                            break;

                    }
                }

                //-----------参数修改的反馈------------//
                if (protocolByte[3 + offset] == 165 && protocolByte[4 + offset] == 90)//参数好修改
                {
                    int jp = 1;
                }
                else if (protocolByte[3 + offset] == 85 && protocolByte[4 + offset] == 170)//单地址修改
                {
                    int jt = 1;
                }
                else
                {
                }

            }

        }

        public void StartFilterPacketProcess(int channelIndex, int commHighIndex, int commLowIndex, int packetType)
        {
            lock (this.m_filterCriSection)
            {
                if (channelIndex == -1)
                {
                    this.m_filterSymbol[0] = true;
                }
                else
                {
                    this.m_filterSymbol[0] = false;
                    this.m_channelIndex = (byte)channelIndex;
                }
                if (commHighIndex == -1)
                {
                    this.m_filterSymbol[1] = true;
                }
                else
                {
                    this.m_filterSymbol[1] = false;
                    this.m_commHighIndex = (byte)commHighIndex;
                }
                if (commLowIndex == -1)
                {
                    this.m_filterSymbol[2] = true;
                }
                else
                {
                    this.m_filterSymbol[2] = false;
                    this.m_commLowIndex = (byte)commLowIndex;
                }
                if (packetType == -1)
                {
                    this.m_filterSymbol[3] = true;
                }
                else
                {
                    this.m_filterSymbol[3] = false;
                    this.m_packetType = (byte)packetType;
                }

                for (int j = 0; j < this.m_filterPackets.Length; j++)
                {
                    this.m_filterPackets[j] = "";
                    this.m_filterPacketIndex = 0;
                }
            }
        }

        private void SolveFilterPacket(byte[] protocolByte, int len)
        {
            int offset = 0;
            int jk = 0;
            bool[] tmpSymbol = new bool[4];

            if ((len % 11) == 0)
            {
                jk = len / 11;
                for (int j = 0; j < jk; j++)
                {
                    offset = j * 11;
                    for (int jp = 0; jp < 4; jp++)
                    {
                        tmpSymbol[jp] = false;
                    }

                    //--------------------//
                    if (this.m_filterSymbol[0] == true)
                    {
                        tmpSymbol[0] = true;
                    }
                    else
                    {
                        if (protocolByte[0 + offset] == this.m_channelIndex)
                        {
                            tmpSymbol[0] = true;
                        }
                    }
                    if (this.m_filterSymbol[1] == true)
                    {
                        tmpSymbol[1] = true;
                    }
                    else
                    {
                        if (protocolByte[1 + offset] == this.m_commHighIndex)
                        {
                            tmpSymbol[1] = true;
                        }
                    }
                    if (this.m_filterSymbol[2] == true)
                    {
                        tmpSymbol[2] = true;
                    }
                    else
                    {
                        if (protocolByte[2 + offset] == this.m_commLowIndex)
                        {
                            tmpSymbol[2] = true;
                        }
                    }
                    if (this.m_filterSymbol[3] == true)
                    {
                        tmpSymbol[3] = true;
                    }
                    else
                    {
                        if (protocolByte[3 + offset] == this.m_packetType)
                        {
                            tmpSymbol[3] = true;
                        }
                    }

                    //---------------------//
                    if (tmpSymbol[0] == true && tmpSymbol[1] == true && tmpSymbol[2] == true && tmpSymbol[3] == true)
                    {
                        RefreshFilterDataBuffer(protocolByte, offset);
                    }
                }
            }
        }

        private void RefreshFilterDataBuffer(byte[] protocolByte, int offset)
        {
            string tmpText = "";

            lock (this.m_filterCriSection)
            {
                tmpText = "";
                for (int jt = 0; jt < 11; jt++)
                {
                    tmpText = tmpText + "【" + protocolByte[jt + offset].ToString().Trim() + "】-";
                }

                this.m_filterPackets[this.m_filterPacketIndex] = tmpText.Trim();
                if (this.m_filterPackets[this.m_filterPacketIndex].Length > 1)
                {
                    this.m_filterPackets[this.m_filterPacketIndex] = this.m_filterPackets[this.m_filterPacketIndex].Substring(0, this.m_filterPackets[this.m_filterPacketIndex].Length - 1);
                }
                if ((this.m_filterPacketIndex + 1) < this.m_filterPackets.Length)
                {
                    this.m_filterPacketIndex = this.m_filterPacketIndex + 1;
                }
                else
                {
                    this.m_filterPacketIndex = 0;
                }
            }

        }

        public void GetFilterPacket(string[] fiterPackets)
        {
            int jk = 0;

            lock (this.m_filterCriSection)
            {
                for (int j = this.m_filterPacketIndex; j < this.m_filterPackets.Length; j++)
                {
                    fiterPackets[jk] = this.m_filterPackets[j];
                    jk = jk + 1;
                }

                for (int j = 0; j < this.m_filterPacketIndex; j++)
                {
                    fiterPackets[jk] = this.m_filterPackets[j];
                    jk = jk + 1;
                }

            }
        }

        private void SolveACToNowData(byte[] protocolByte, int offset)
        {
            int jk;
            int tmpData;

            jk = protocolByte[4 + offset];
            if (jk <= 28)
            {
                tmpData = protocolByte[5 + offset];
                tmpData = tmpData * 256 + protocolByte[6 + offset];
                if (tmpData >= 0)
                {
                    switch (jk)
                    {
                        case 0:

                            break;

                    }

                }
            }

        }

        private void SolveAnodeData(byte[] protocolByte, int offset)
        {
            byte[] bufferTemp_left = new byte[2];
            byte[] bufferTemp_right = new byte[2];
            int jk;

            int dataTempValueLeft;
            int dataTempValueRight;
            int dataSideA;
            int dataSideB;

            //----------------//
            jk = protocolByte[4 + offset];
            if (jk <= 16)  //MainForm.m_paraPassThrough.anodeNodesNumber_24pads =17
            {
                dataTempValueLeft = protocolByte[5 + offset] & 127;
                dataTempValueLeft = dataTempValueLeft * 256 + protocolByte[6 + offset];
                bufferTemp_left[0] = Convert.ToByte(dataTempValueLeft / 256);
                bufferTemp_left[1] = Convert.ToByte(dataTempValueLeft % 256);
                if ((protocolByte[5 + offset] & 128) != 0)  //正负
                {
                    dataTempValueLeft = (-1) * dataTempValueLeft;
                }

                dataTempValueRight = protocolByte[7 + offset] & 127;
                dataTempValueRight = dataTempValueRight * 256 + protocolByte[8 + offset];
                bufferTemp_right[0] = Convert.ToByte(dataTempValueRight / 256);
                bufferTemp_right[1] = Convert.ToByte(dataTempValueRight % 256);
                if ((protocolByte[7 + offset] & 128) != 0)
                {
                    dataTempValueRight = (-1) * dataTempValueRight;
                }

                switch (jk)
                {
                    case 0://A1,B1
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 1://A2,B2
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 2://A3,B3
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 3://A4,B4
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 4://A5,B5
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 5://A6,B6
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 6://A7,B7
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 7://A8,B8
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 8://A9,B9
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 9://A10,B10
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 10://A11,B11
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 11://A12,B12
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 12://A13,B13
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 13://A14,B14
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 14://A15,B15
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 15://A16,B16
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    case 16://A17,B17
                        dataSideA = dataTempValueLeft;
                        dataSideB = dataTempValueRight;
                        break;
                    default:
                        break;
                }

            }

        }

        private void SolvePotTempData(byte[] protocolByte, int offset)
        {
            int jk;
            int tmpData;

            jk = protocolByte[4 + offset];
            if (jk <= 255)
            {
                tmpData = protocolByte[5 + offset];
                tmpData = tmpData * 256 + protocolByte[6 + offset];
                if (tmpData >= 0)
                {
                    if (jk <= 15)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else if (jk <= 31)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else if (jk <= 47)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else if (jk <= 63)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else if (jk <= 79)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else if (jk <= 95)
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }
                    else
                    {
                        this.m_tempTestData[jk] = tmpData;
                    }

                }
            }

        }

        private void SolveSmokeGasData(byte[] protocolByte, int offset)
        {
            int jk;
            int tmpData;

            jk = protocolByte[4 + offset];
            if (jk <= 255)
            {
                tmpData = protocolByte[5 + offset];
                tmpData = tmpData * 256 + protocolByte[6 + offset];
                if (tmpData >= 0)
                {
                    if (jk == 0)
                    {
                        CommStreamData.m_gasSmokeData = tmpData;
                    }
                }
            }

        }

        private void SolveAddressParaData(byte[] protocolByte)
        {
            int jk;
            long addressTemp;
            int potCommIndex = 0;

            potCommIndex = protocolByte[1] * 8;
            potCommIndex = potCommIndex + (protocolByte[2] & 224) / 32;

            jk = (protocolByte[2] & 15) - 3;//去掉地址两个字节
            addressTemp = protocolByte[4];
            addressTemp = addressTemp * 256 + protocolByte[5];
            addressTemp = addressTemp - addressParaModify.m_startAddress;

            if (addressTemp >= 0)
            {
                if (addressParaModify.m_commMode == 0)
                {
                    for (int i = 0; i < jk; i++)
                    {
                        if ((addressTemp + i) <= (48 - 1))
                        {
                            addressParaModify.addressParaBuffer[addressTemp + i].SetIntType(protocolByte[6 + i]);
                        }
                    }
                }
                else if (addressParaModify.m_commMode == 1)
                {
                    if (protocolByte[0] == addressParaModify.m_channelIndex_mix && potCommIndex == addressParaModify.m_commIndex_mix)
                    {
                        for (int i = 0; i < jk; i++)
                        {
                            if ((addressTemp + i) <= (48 - 1))
                            {
                                addressParaModify.addressParaBuffer[addressTemp + i].SetIntType(protocolByte[6 + i]);
                            }
                        }
                    }
                }
                else
                {
                }
            }
        }

        private void DynamicDataExchange()//动态曲线用数据移动函数
        {
            int jp;

            try
            {
                jp = GetDynamicCountIndex();


            }
            catch
            {
            }
            finally
            {
                jp = GetDynamicCountIndex();
                SetDynamicCountIndexOrigin(jp);
                jp = jp + 1;
                if (jp > (CommNetConstants.AnodeDynamicBufferLength - 1))
                {
                    SetDynamicCountIndex(0);
                }
                else
                {
                    SetDynamicCountIndex(jp);
                }
            }

        }

        private void SolveProtocolDataWithoutVerify(byte[] protocolByte)
        {
            int tempDataValue;
            int offset = 0;

            if (MainWindow.m_packetFilterSymbol == false)
            {
                this.m_packetIndex = protocolByte[3 + offset];
                this.m_effectPacketLength = protocolByte.Length;
                Buffer.BlockCopy(protocolByte, 0, this.m_packetDataBuffer, 0, this.m_effectPacketLength);
            }
            else
            {
                if (MainWindow.m_packetIndexSpecial != 0)
                {
                    if (protocolByte[3 + offset] == MainWindow.m_packetIndexSpecial)
                    {
                        this.m_packetIndex = protocolByte[3 + offset];
                        this.m_effectPacketLength = protocolByte.Length;
                        Buffer.BlockCopy(protocolByte, 0, this.m_packetDataBuffer, 0, this.m_effectPacketLength);
                    }
                }
            }

            //-----------------------//
            switch (protocolByte[3 + offset])
            {
                case 3:
                case 4:

                    tempDataValue = protocolByte[4 + offset];
                    tempDataValue = tempDataValue * 256 + protocolByte[5 + offset];
                    this.m_potRealVolt = tempDataValue;

                    tempDataValue = protocolByte[6 + offset];
                    tempDataValue = tempDataValue * 256 + protocolByte[7 + offset];
                    this.m_potSerialCurrent = tempDataValue;

                    break;

                case 5:
                case 6:

                    this.m_potDisplayInfo[3].PocellDataValue = protocolByte[10 + offset];
                    break;

                case 7:

                    this.m_potTroubleInfo[0].PocellDataValue = protocolByte[4 + offset];
                    this.m_potTroubleInfo[1].PocellDataValue = protocolByte[5 + offset];
                    this.m_potTroubleInfo[2].PocellDataValue = protocolByte[6 + offset];
                    this.m_potTroubleInfo[3].PocellDataValue = protocolByte[7 + offset];

                    this.m_potDisplayInfo[0].PocellDataValue = protocolByte[8 + offset];
                    this.m_potDisplayInfo[1].PocellDataValue = protocolByte[9 + offset];
                    this.m_potDisplayInfo[2].PocellDataValue = protocolByte[10 + offset];

                    break;

                case 11:

                    this.m_potSwitchInfo[0].PocellDataValue = protocolByte[5 + offset];
                    this.m_potSwitchInfo[1].PocellDataValue = protocolByte[4 + offset];

                    break;

                case 254:

                    break;

                default:
                    break;

            }

        }

        public void CleanStreamBuffer()
        {
            for (int j = 0; j < this.m_tempTestData.Length; j++)
            {
                this.m_tempTestData[j] = 0;
            }

            this.m_potRecQueue_anode.PopAllPacket();

            for (int j = 0; j < this.m_mainAnodeData.Length; j++)
            {
                this.m_mainAnodeData[j].AnodeDataValue = 0f;
                this.m_auxiliaryAnodeData[j].AnodeDataValue = 0f;
                this.m_sideAnodeData[j].AnodeDataValue = 0f;
            }

            for (int j = 0; j < this.m_anodeDynamicDataBuffer.Length; j++)
            {
                for (int k = 0; k < this.m_mainAnodeData.Length; k++)
                {
                    this.m_anodeDynamicDataBuffer[j].m_mainAnodeData[k].AnodeDataValue = 0f;
                    this.m_anodeDynamicDataBuffer[j].m_auxiliaryAnodeData[k].AnodeDataValue = 0f;
                    this.m_anodeDynamicDataBuffer[j].m_sideAnodeData[k].AnodeDataValue = 0f;
                }
            }

        }

        private void CheckDataPacket_list(byte[] dataBuffer, int len)
        {
            if (len < this.m_packetDataBuffer.Length)
            {
                SolveProtocolData(dataBuffer, len);
            }
            else
            {
            }
        }

        public void PushDataIntoListBuffer(byte[] dataBuffer, int offset, int len)
        {
            int tmpIndex;
            lock (this.m_criBufferSolve)
            {
                tmpIndex = this.m_buffer.Count;
                for (int j = 0; j < len; j++)
                {
                    this.m_buffer.Insert(tmpIndex + j, dataBuffer[offset + j]);
                }
            }
        }

        private void DeserializePotcellData()
        {
            int tmpData = 0;
            bool tmpSymbol = false;

            lock (this.m_criBufferSolve)
            {
                this.m_buffetCount = this.m_buffer.Count;
                if (this.m_buffer.Count > 17)
                {
                    if (this.m_buffer[0] == 170 && this.m_buffer[1] == 170)
                    {
                        tmpData = this.m_buffer[2];
                        tmpData = tmpData * 256 + this.m_buffer[3];
                        if (this.m_buffer.Count < (tmpData + 6))
                        {
                        }
                        else
                        {
                            if (this.m_buffer[tmpData + 4] == 85 && this.m_buffer[tmpData + 5] == 85)
                            {
                                tmpSymbol = true;
                                if (tmpData > this.m_buffer_proxy.Length)
                                {
                                    this.m_buffer_proxy = new byte[tmpData];
                                }
                                this.m_buffer.CopyTo(4, this.m_buffer_proxy, 0, tmpData);
                                this.m_buffer.RemoveRange(0, tmpData + 6);
                            }
                            else
                            {
                                this.m_buffer.RemoveRange(0, tmpData + 6);
                            }
                        }
                    }
                    else
                    {
                        this.m_buffer.RemoveRange(0, 2);
                    }
                }
            }

            //---------------------//
            if (tmpSymbol == true && tmpData > 0)
            {
                CheckDataPacket_list(this.m_buffer_proxy, tmpData);
            }

        }

        internal ReceiveQueueBuffer_packet AnodeQueueReceive
        {
            get
            {
                return this.m_potRecQueue_anode;
            }
        }

        internal AnodeDynamicDataBuffer[] AnodeDynamicDataBuffer
        {
            get
            {
                return this.m_anodeDynamicDataBuffer;
            }
        }

        public int AnodePodIndex
        {
            get
            {
                lock (this.m_criSection)
                {
                    return this.m_anodePodIndex;
                }
            }
            set
            {
                lock (this.m_criSection)
                {
                    this.m_anodePodIndex = value;

                }
            }
        }

        public bool CommCheckSymbol
        {
            get
            {
                lock (this.m_criCommSection)
                {
                    return this.m_commCheckSymbol;
                }
            }
            set
            {
                lock (this.m_criCommSection)
                {
                    this.m_commCheckSymbol = value;
                }
            }
        }

    }

}
